package com.smartphonedev.receiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

import com.smartphonedev.function.Ringtones;
import com.smartphonedev.function.Storage;
import com.smartphonedev.activity.R;
import com.smartphonedev.function.Notify;

public class SmsReceiver extends BroadcastReceiver {

	private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
	private static boolean isTheFirst = true;
	private NotificationManager NM;
	private String getFormatSms = "format";
	private String getFormatSmsB = "confirmformat";
	private String getFormatSms2 = "getformat";
	private String getFormatSms3 = "receiverformat";
	private String getFormatSms4 = "sendcode";

	@Override
	public void onReceive(Context context, Intent intent) {

		// Receiver share
		if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
			final SmsMessage smsMessage = extractSmsMessage(intent);
			if(smsMessage != null ){
				String str = smsMessage.getMessageBody();
				str = str.replace(";", ":");
				String[] arr = str.split(":");
				String SMS_FORMAT = getFormatSms.toUpperCase(); 
				if(arr != null && SMS_FORMAT.equals(arr[0].trim().toUpperCase())){
					NM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
					Notify.showNotification(context, NM, context.getString(R.string.androidnotify));
					Ringtones.play2(context, R.raw.notifysuccess);
					Storage.shareNew(context);
				}else {

				}
			}
		}
		if(isTheFirst){

			isTheFirst = false;
		}

		// Confirm share
		if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
			final SmsMessage smsMessage = extractSmsMessage(intent);
			if(smsMessage != null ){
				String str = smsMessage.getMessageBody();
				str = str.replace(";", ":");
				String[] arr = str.split(":");
				String SMS_FORMAT = getFormatSmsB.toUpperCase(); 
				if(arr != null && SMS_FORMAT.equals(arr[0].trim().toUpperCase())){
					NM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
					Notify.showNotification(context, NM, context.getString(R.string.androidnotifyb));
					Ringtones.play2(context, R.raw.notifysuccess);
					Storage.confirmShare(context);
				}else {

				}
			}
		}
		if(isTheFirst){

			isTheFirst = false;
		}


		// Receiver request
		if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
			final SmsMessage smsMessage = extractSmsMessage(intent);
			if(smsMessage != null ){
				String str = smsMessage.getMessageBody();
				str = str.replace(";", ":");
				String[] arr = str.split(":");
				String SMS_FORMAT = getFormatSms2.toUpperCase(); 
				if(arr != null && SMS_FORMAT.equals(arr[0].trim().toUpperCase())){
					NM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
					Notify.showNotification(context, NM, context.getString(R.string.androidnotify2));
					Ringtones.play2(context, R.raw.notifysuccess);
					Storage.requestNew(context);
				}else {

				}
			}
		}
		if(isTheFirst){

			isTheFirst = false;
		}



		// Receiver request accept
		if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
			final SmsMessage smsMessage = extractSmsMessage(intent);
			if(smsMessage != null ){
				String str = smsMessage.getMessageBody();
				str = str.replace(";", ":");
				String[] arr = str.split(":");
				String SMS_FORMAT = getFormatSms3.toUpperCase(); 
				if(arr != null && SMS_FORMAT.equals(arr[0].trim().toUpperCase())){
					NM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
					Notify.showNotification(context, NM, context.getString(R.string.androidnotify3));
					Ringtones.play2(context, R.raw.notifysuccess);
					Storage.requestAccept(context);
				}else {

				}
			}
		}

		if(isTheFirst){

			isTheFirst = false;
		}

		// Receiver from tracking
		if (intent != null && SMS_RECEIVED.equals(intent.getAction())) {
			final SmsMessage smsMessage = extractSmsMessage(intent);
			if(smsMessage != null ){
				String str = smsMessage.getMessageBody();
				str = str.replace(";", ":");
				String[] arr = str.split(":");
				String SMS_FORMAT = getFormatSms4.toUpperCase(); 
				if(arr != null && SMS_FORMAT.equals(arr[0].trim().toUpperCase())){
					NM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
					Notify.showNotification2(context, NM, context.getString(R.string.androidnotify4));
					Ringtones.play2(context, R.raw.notifysuccess);
					Storage.openKeyTracking(context);
				}else {

				}
			}
		}
		if(isTheFirst){

			isTheFirst = false;
		}	

	}

	private SmsMessage extractSmsMessage(final Intent intent) {

		final Bundle pudsBundle = intent.getExtras();
		final Object[] pdus = (Object[]) pudsBundle.get("pdus");
		final SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[0]);

		return smsMessage;

	}

}


